﻿using BotDetector.Core.Model;

namespace BotDetector.Core.Detector
{
    public class ExactPhraseAnalyzor : IAnalyzer
    {
        private BotLength typeOfLength;
        private KeyWordsAnalyzor keyWordsAnalyzor;

        public ExactPhraseAnalyzor(BotLength typeLength)
        {
            this.typeOfLength = typeLength;
        }
        public bool AnalyzeAfterExactPhrase(ResultItemList resultItemList)
        {
            int exactPhrasesFound = CountResultsFoundWithExactPhrase(resultItemList);
            if (DetectValueForEachTypeOfBot(exactPhrasesFound, 3, 2, 1))
            {
                return true;
            }

            keyWordsAnalyzor = new KeyWordsAnalyzor(typeOfLength);
            return keyWordsAnalyzor.AnalyzeAfterKeyWords(resultItemList);
        }
        public int CountResultsFoundWithExactPhrase(ResultItemList resultItemList)
        {
            int exactPhrasesFound = 0;
            const string newLine = "\n";
            foreach (var webResult in resultItemList)
            {
                string title = webResult.Title.Replace(newLine, string.Empty);
                string content = webResult.Description.Replace(newLine, string.Empty);
                if (title.Contains(resultItemList.Phrase) || content.Contains(resultItemList.Phrase))
                {
                    exactPhrasesFound++;
                }
            }
            return exactPhrasesFound;
        }
        public bool DetectValueForEachTypeOfBot(int factor, int shortValue, int mediumValue, int longValue)
        {
            switch (typeOfLength)
            {
                case BotLength.Short:
                    if (factor >= shortValue)
                    {
                        return true;
                    }
                    break;
                case BotLength.Medium:
                    if (factor >= mediumValue)
                    {
                        return true;
                    }
                    break;
                case BotLength.Long:
                    if (factor >= longValue)
                    {
                        return true;
                    }
                    break;
                default:
                    return false;
            }
            return false;
        }
        public bool Analyze(ResultItemList resultItemList)
        {
            return AnalyzeAfterExactPhrase(resultItemList);
        }

    }
}
